package com.ourteam.product.model.api;

import java.io.File;
import java.io.FileOutputStream;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.sql.DataSource;

import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.SystemUtils;
import org.apache.ddlutils.Platform;
import org.apache.ddlutils.PlatformFactory;
import org.apache.ddlutils.io.DatabaseIO;
import org.apache.ddlutils.model.Column;
import org.apache.ddlutils.model.Database;
import org.apache.ddlutils.model.Table;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;
import org.springframework.jdbc.datasource.SingleConnectionDataSource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ourteam.frame.controller.AbstractServiceController;
import com.ourteam.frame.web.AbstractNoQueryBeanSearchHelper;
import com.ourteam.frame.web.AbstractSearchHelper;
import com.ourteam.frame.web.GridBean;
import com.ourteam.frame.web.ProgressInfoBean;
import com.ourteam.frame.web.RequestStatus;
import com.ourteam.product.model.dao.ProductDatabaseDAOQueryBean;
import com.ourteam.product.model.dao.ProductModelObjectDAOQueryBean;
import com.ourteam.product.model.dao.ProductModelPropertyDAOQueryBean;
import com.ourteam.product.model.domain.ProductDatabaseBean;
import com.ourteam.product.model.domain.ProductModelObjectBean;
import com.ourteam.product.model.domain.ProductModelPropertyBean;
import com.ourteam.product.model.domain.ProductModelTypeEnum;
import com.ourteam.product.model.service.IProductModelService;
import com.ourteam.product.model.service.ProductModelServiceFactory;

@Controller
@CrossOrigin
@RequestMapping("/api/PRODUCT/MODEL/productDatabase")
public class ProductDatabaseAPI extends AbstractServiceController {

	private static final IProductModelService PRODUCT_MODEL_SERVICE = ProductModelServiceFactory
			.getProductModelService();

	@RequestMapping("/queryProductDatabases")
	@ResponseBody
	public GridBean queryProductDatabases(final @ModelAttribute ProductDatabaseSearchModel searchModel,
			HttpServletRequest request, Model model) throws Exception {

		AbstractSearchHelper<ProductDatabaseDAOQueryBean, ProductDatabaseSearchModel> searchHelper = new AbstractSearchHelper<ProductDatabaseDAOQueryBean, ProductDatabaseSearchModel>(
				searchModel) {

			@Override
			protected ProductDatabaseDAOQueryBean createQueryBean() {
				return new ProductDatabaseDAOQueryBean();
			}

			@Override
			protected int queryCount(ProductDatabaseDAOQueryBean queryBean) throws Exception {
				return PRODUCT_MODEL_SERVICE.queryProductDatabaseCount(queryBean);
			}

			@Override
			protected Object[] queryData(ProductDatabaseDAOQueryBean queryBean) throws Exception {
				return PRODUCT_MODEL_SERVICE.queryProductDatabases(queryBean);
			}

		};

		return searchHelper.doSearch(request);

	}

	@RequestMapping("/getProductDatabases")
	@ResponseBody
	public ProductDatabaseBean[] getProductDatabases() throws Exception {
		return PRODUCT_MODEL_SERVICE.queryProductDatabases(new ProductDatabaseDAOQueryBean());
	}

	@RequestMapping("/getProductDatabaseById")
	@ResponseBody
	public ProductDatabaseBean getProductDatabaseById(@RequestParam long id) throws Exception {
		return PRODUCT_MODEL_SERVICE.getProductDatabaseById(id);
	}

	@RequestMapping("/saveProductdatabase")
	@ResponseBody
	public ProductDatabaseBean saveProductdatabase(@ModelAttribute ProductDatabaseBean databaseBean) throws Exception {

		if (databaseBean.getId() == 0) {
			PRODUCT_MODEL_SERVICE.addNewProductDatabase(databaseBean);
		} else {
			PRODUCT_MODEL_SERVICE.modifyProductDatabase(databaseBean);
		}

		return databaseBean;
	}

	@RequestMapping("/deleteProductDatabase")
	@ResponseBody
	public boolean deleteProductDatabase(@RequestParam(value = "ids") long[] ids) throws Exception {
		PRODUCT_MODEL_SERVICE.deleteProductDatabases(ids);
		return true;
	}

	public static SingleConnectionDataSource getSingleConnectionDataSource(ProductDatabaseBean databaseServerBean) {

		SingleConnectionDataSource dataSource = new SingleConnectionDataSource();

		dataSource.setUsername(databaseServerBean.getDatabaseUser());
		dataSource.setPassword(databaseServerBean.getDatabasePassword());
		dataSource.setSuppressClose(true);

		if ("mysql".equalsIgnoreCase(databaseServerBean.getDatabaseType())) {

			dataSource.setDriverClassName("com.mysql.jdbc.Driver");

			dataSource.setUrl("jdbc:mysql://" + databaseServerBean.getDatabaseIp() + ":"
					+ databaseServerBean.getDatabasePort() + "/" + databaseServerBean.getDatabaseServiceId()
					+ "?useSSL=false&serverTimezone=UTC&useUnicode=true&characterEncoding=UTF-8");
		} else if ("oracle".equalsIgnoreCase(databaseServerBean.getDatabaseType())) {

			dataSource.setDriverClassName("oracle.jdbc.driver.OracleDriver");

			dataSource.setUrl("jdbc:oracle:thin:@" + databaseServerBean.getDatabaseIp() + ":"
					+ databaseServerBean.getDatabasePort() + ":" + databaseServerBean.getDatabaseServiceId());
		} else if ("sqlserver".equalsIgnoreCase(databaseServerBean.getDatabaseType())) {

			dataSource.setDriverClassName("com.microsoft.sqlserver.jdbc.SQLServerDriver");

			dataSource.setUrl("jdbc:sqlserver://" + databaseServerBean.getDatabaseIp() + ":"
					+ databaseServerBean.getDatabasePort() + ";databaseName="
					+ databaseServerBean.getDatabaseServiceId());
		} else if ("sqlite".equalsIgnoreCase(databaseServerBean.getDatabaseType())) {
			dataSource.setDriverClassName("org.sqlite.JDBC");
			dataSource.setUrl("jdbc:sqlite://" + databaseServerBean.getDatabaseIp()
					+ databaseServerBean.getDatabaseServiceId() + ".sqlite");
		}

		return dataSource;

	}

	protected DatabaseTableBean[] searchDatabaseTables(long databaseId, String tableName) throws Exception {

		ProductDatabaseBean databaseBean = PRODUCT_MODEL_SERVICE.getProductDatabaseById(databaseId);

		SingleConnectionDataSource dataSource = getSingleConnectionDataSource(databaseBean);

		dataSource.setSuppressClose(true);

		Platform platform = PlatformFactory.createNewPlatformInstance(dataSource);

		Database database = platform.readModelFromDatabase(databaseBean.getDatabaseServiceId(), null,
				StringUtils.upperCase(databaseBean.getDatabaseUser()), null);

		Table[] tables = new Table[0];
		
		if (StringUtils.isNotBlank(tableName)) {
			StringBuffer regEx = new StringBuffer(".*");
			regEx.append(tableName);
			regEx.append(".*");
			tables = database.findTables(regEx.toString(), true);
		} else {

			tables = database.getTables();
		}

		dataSource.destroy();

		DatabaseTableBean[] tableBeans = new DatabaseTableBean[tables.length];

		for (int i = 0; i < tableBeans.length; i++) {
			tableBeans[i] = new DatabaseTableBean();
			tableBeans[i].setName(tables[i].getName());
			tableBeans[i].setType(tables[i].getType());
			tableBeans[i].setDescription(tables[i].getDescription());
			Column[] columns = tables[i].getColumns();
			DatabaseTableColumnBean[] columnBeans = new DatabaseTableColumnBean[columns.length];
			for (int j = 0; j < columns.length; j++) {
				Column column = columns[j];
				columnBeans[j] = new DatabaseTableColumnBean();

				columnBeans[j].setName(column.getName());
				columnBeans[j].setType(column.getType());
				columnBeans[j].setDescription(column.getDescription());
				columnBeans[j].setAutoIncrement(column.isAutoIncrement());
				columnBeans[j].setPrimaryKey(column.isPrimaryKey());
				columnBeans[j].setRequired(column.isRequired());
				columnBeans[j].setScale(column.getScale());
				columnBeans[j].setSizeAsInt(column.getSizeAsInt());
				columnBeans[j].setTypeCode(column.getTypeCode());
				columnBeans[j].setDefaultValue(column.getDefaultValue());

			}

			tableBeans[i].setColumnBeans(columnBeans);
		}

		return tableBeans;
	}

	protected DatabaseTableBean[] getDatabaseTables(long databaseId, String[] tableNames) throws Exception {

		ProductDatabaseBean databaseBean = PRODUCT_MODEL_SERVICE.getProductDatabaseById(databaseId);

		SingleConnectionDataSource dataSource = getSingleConnectionDataSource(databaseBean);

		dataSource.setSuppressClose(true);

		Platform platform = PlatformFactory.createNewPlatformInstance(dataSource);

		Database database = platform.readModelFromDatabase(databaseBean.getDatabaseServiceId(), null,
				StringUtils.upperCase(databaseBean.getDatabaseUser()), null);

		Table[] tables = new Table[tableNames.length];

		for (int i = 0; i < tables.length; i++) {
			tables[i] = database.findTable(tableNames[i]);

		}

		dataSource.destroy();

		DatabaseTableBean[] tableBeans = new DatabaseTableBean[tables.length];

		for (int i = 0; i < tableBeans.length; i++) {
			tableBeans[i] = new DatabaseTableBean();
			tableBeans[i].setName(tables[i].getName());
			tableBeans[i].setType(tables[i].getType());
			tableBeans[i].setDescription(tables[i].getDescription());
			Column[] columns = tables[i].getColumns();
			DatabaseTableColumnBean[] columnBeans = new DatabaseTableColumnBean[columns.length];
			for (int j = 0; j < columns.length; j++) {
				Column column = columns[j];
				columnBeans[j] = new DatabaseTableColumnBean();

				columnBeans[j].setName(column.getName());
				columnBeans[j].setType(column.getType());
				columnBeans[j].setDescription(column.getDescription());
				columnBeans[j].setAutoIncrement(column.isAutoIncrement());
				columnBeans[j].setPrimaryKey(column.isPrimaryKey());
				columnBeans[j].setRequired(column.isRequired());
				columnBeans[j].setScale(column.getScale());
				columnBeans[j].setSizeAsInt(column.getSizeAsInt());
				columnBeans[j].setTypeCode(column.getTypeCode());
				columnBeans[j].setDefaultValue(column.getDefaultValue());

			}

			tableBeans[i].setColumnBeans(columnBeans);
		}

		return tableBeans;
	}

	@RequestMapping("/queryDatabaseTables")
	@ResponseBody
	public GridBean queryDatabaseTables(@ModelAttribute ProductDatabaseTableSearchModel searchModel,
			HttpServletRequest request, Model model) throws Exception {

		AbstractNoQueryBeanSearchHelper<ProductDatabaseTableSearchModel> searchHelper = new AbstractNoQueryBeanSearchHelper<ProductDatabaseTableSearchModel>(
				searchModel) {

			@Override
			protected Object[] getData(ProductDatabaseTableSearchModel searchModel) throws Exception {
				try {
					return searchDatabaseTables(searchModel.getDatabaseId(), searchModel.getTableName());
				} catch (Exception e) {
					return ArrayUtils.EMPTY_OBJECT_ARRAY;
				}

			}
		};

		return searchHelper.doSearch(request);

	}
	
	@RequestMapping("/checkImportDatabaseTables")
	@ResponseBody
	public ProgressInfoBean checkImportDatabaseTables(@RequestParam long databaseId, final HttpServletRequest request)
			throws Exception {

		return (ProgressInfoBean) request.getSession().getAttribute("importDataTables" + databaseId);

	}

	@RequestMapping("/importDatabaseTables")
	@ResponseBody
	public RequestStatus importDatabaseTables(@RequestParam final long packageId, @RequestParam final long databaseId,
			@RequestParam(value = "tables") String[] tables, final HttpServletRequest request) throws Exception {

		final DatabaseTableBean[] tableBeans = getDatabaseTables(databaseId, tables);

		final RequestStatus requestStatus = new RequestStatus();

		final long submitterId = getLoginUserId();

		final ProgressInfoBean progressInfoBean = new ProgressInfoBean();

		progressInfoBean.setId("importDataTables" + databaseId);

		progressInfoBean.setTask("导入数据库表");

		progressInfoBean.setUnkownProgress(false);

		progressInfoBean.setProgressMessage("导入中，请稍候...");

		progressInfoBean.setTotalCount(tableBeans.length);

		progressInfoBean.setValue(0);

		requestStatus.setRequestId(progressInfoBean.getId());

		request.getSession().setAttribute(progressInfoBean.getId(), progressInfoBean);

		for (int i = 0; i < tableBeans.length; i++) {
			try {
				progressInfoBean.setProgressMessage(tableBeans[i].getName() + " " + (i + 1) + "/" + tableBeans.length);
				// requestStatus.setMessage(tableBeans[i].getName() + " " + (i + 1) + "/" +
				// tableBeans.length);
				// session.setAttribute(requestId, requestStatus);
				processDatabaseTableImport(packageId, tableBeans[i], submitterId);
				progressInfoBean.setValue(i);
				requestStatus.setSuccess(true);
				
			} catch (Exception e) {
				requestStatus.setMessage(e.getMessage());
				requestStatus.setFinished(true);
				requestStatus.setSuccess(false);
				e.printStackTrace();
			}
		}
		request.getSession().removeAttribute(progressInfoBean.getId());
		requestStatus.setFinished(true);

		return requestStatus;
	}

	@RequestMapping("/checkUpdateDatabaseTables")
	@ResponseBody
	public ProgressInfoBean checkUpdateDatabaseTables(@RequestParam long databaseId, final HttpServletRequest request)
			throws Exception {

		return (ProgressInfoBean) request.getSession().getAttribute("updateDatabase" + databaseId);

	}

	@RequestMapping("/updateDatabaseTables")
	@ResponseBody
	public RequestStatus updateDatabaseTables(@RequestParam long databaseId, @RequestParam long packageId,
			final HttpServletRequest request) throws Exception {

		final RequestStatus requestStatus = new RequestStatus();

		final ProgressInfoBean progressInfoBean = new ProgressInfoBean();

		progressInfoBean.setId("updateDatabase" + databaseId);

		progressInfoBean.setTask("更新数据库结构");

		progressInfoBean.setUnkownProgress(true);

		progressInfoBean.setProgressMessage("更新中，请稍候...");

		requestStatus.setRequestId(progressInfoBean.getId());

		request.getSession().setAttribute(progressInfoBean.getId(), progressInfoBean);

		ProductDatabaseDAOQueryBean queryDatabaseBean = new ProductDatabaseDAOQueryBean();

		queryDatabaseBean.createCriteria().andIdEqualTo(databaseId);

		final ProductDatabaseBean databaseBean = PRODUCT_MODEL_SERVICE.getProductDatabaseById(databaseId);

		final File databaseXmlFile = getDatabaseTableXMLFile(packageId, databaseBean.getDatabaseServiceId());

		final DataSource dataSource = getSingleConnectionDataSource(databaseBean);

		if (databaseBean != null) {

			DatabaseIO databaseIo = new DatabaseIO();
			databaseIo.setValidateXml(false);
			Database database = databaseIo.read(databaseXmlFile);

			Platform platform = PlatformFactory.createNewPlatformInstance(dataSource);

			try {

				Database changeDatabase = new Database();

				Database targetDatabase = platform.readModelFromDatabase(databaseBean.getDatabaseName());

				changeDatabase.setName(targetDatabase.getName());
				changeDatabase.setIdMethod(targetDatabase.getIdMethod());
				changeDatabase.setVersion(targetDatabase.getVersion());

				Table[] tables = database.getTables();

				for (int i = 0; i < tables.length; i++) {

					Table targetTable = targetDatabase.findTable(tables[i].getName(), false);

					if (targetTable != null) {
						changeDatabase.addTable(targetTable);
					}

				}

				if (changeDatabase.getTableCount() == 0) {
					platform.createModel(database, true, false);
				} else {
					platform.alterModel(database, changeDatabase, false);
				}

				requestStatus.setFinished(true);
				requestStatus.setSuccess(true);
				request.getSession().removeAttribute(progressInfoBean.getId());
			} catch (Exception e) {
				requestStatus.setFinished(true);
				requestStatus.setSuccess(false);
				requestStatus.setMessage(e.getMessage());
				request.getSession().removeAttribute(progressInfoBean.getId());
				e.printStackTrace();
			}

		}

		return requestStatus;

	}

	/*
	 * // sqlite 解析xml数据定义文件 public List<String> analysisDatabaseTableXMLFile(String
	 * path) throws Exception{ // 创建saxReader对象 SAXReader reader = new SAXReader();
	 * // 通过read方法读取一个文件 转换成Document对象 Document document = reader.read(new
	 * File(path)); //获取根节点元素对象 Element node = document.getRootElement(); //获取子节点
	 * Element element = node.element("table"); Element ele =
	 * node.element("column");
	 * 
	 * 
	 * }
	 */
	// 生成xml数据定义文件
	public File getDatabaseTableXMLFile(long packageId, String databaseName) throws Exception {

		ProductModelObjectDAOQueryBean queryObjectBean = new ProductModelObjectDAOQueryBean();

		queryObjectBean.createCriteria().andPackageIdEqualTo(packageId).andObjectTypeEqualTo("DB");

		ProductModelObjectBean[] objectBean = PRODUCT_MODEL_SERVICE.queryProductModelObjects(queryObjectBean);
		// 创建根节点database ;
		// Element root = DocumentHelper.createElement("database");
		Document document = DocumentHelper.createDocument();
		Element root = document.addElement("database", "http://db.apache.org/ddlutils/schema/1.1");
		root.addAttribute("name", databaseName);
		// root.addAttribute("xmlns","http://db.apache.org/ddlutils/schema/1.1");

		// 此处 for 循环可替换成 遍历 数据库表的结果集操作;
		for (int i = 0; i < objectBean.length; i++) {
			// 创建子节点
			Element elements = root.addElement("table");
			// 给table节点添加属性 name;
			elements.addAttribute("name", objectBean[i].getObjectName());
			elements.addAttribute("description", objectBean[i].getRemarks());
			// 给 table 节点添加子节点并赋值；
			ProductModelPropertyDAOQueryBean queryPropertyBean = new ProductModelPropertyDAOQueryBean();
			queryPropertyBean.createCriteria().andObjectIdEqualTo(objectBean[i].getId());
			ProductModelPropertyBean[] propertyBean = PRODUCT_MODEL_SERVICE
					.queryProductModelProperties(queryPropertyBean);
			for (int j = 0; j < propertyBean.length; j++) {
				elements.addElement("column").addAttribute("name", propertyBean[j].getPropertyName())
						.addAttribute("autoIncrement", propertyBean[j].getIsAutoInc())
						.addAttribute("size", String.valueOf(propertyBean[j].getDataLength()))
						.addAttribute("type", propertyBean[j].getDataType())
						.addAttribute("required", propertyBean[j].getIsAllowNull())
						.addAttribute("primaryKey", propertyBean[j].getIsPrimaryKey());
			}

		}
		OutputFormat format = new OutputFormat();
		format.setEncoding("UTF-8");// 设置编码格式

		File databaseXmlFile = new File(
				SystemUtils.getJavaIoTmpDir() + "/" + databaseName + System.currentTimeMillis() + ".xml");

		if (databaseXmlFile.exists() == false) {
			databaseXmlFile.createNewFile();
		}

		XMLWriter xmlWriter = new XMLWriter(new FileOutputStream(databaseXmlFile), format);

		xmlWriter.write(document);
		xmlWriter.close();

		return databaseXmlFile;
	}

	@RequestMapping("/getRequestStatus")
	@ResponseBody
	public RequestStatus getRequestStatus(@RequestParam final String requestId, final HttpServletRequest request)
			throws Exception {

		final HttpSession session = request.getSession();

		return (RequestStatus) session.getAttribute(requestId);
	}

	protected void processDatabaseTableImport(long packageId, DatabaseTableBean table, final long submitterId)
			throws Exception {

		String tableName = table.getName().toUpperCase();

		ProductModelObjectDAOQueryBean queryBean = new ProductModelObjectDAOQueryBean();

		queryBean.createCriteria().andPackageIdEqualTo(packageId).andObjectNameEqualTo(tableName)
				.andObjectTypeEqualTo(ProductModelTypeEnum.DB.getName());

		ProductModelObjectBean[] modelObjectBeans = PRODUCT_MODEL_SERVICE.queryProductModelObjects(queryBean);

		if (ArrayUtils.isEmpty(modelObjectBeans)) {

			ProductModelObjectBean persistent = new ProductModelObjectBean();

			persistent.setSubmitterId(submitterId);

			persistent.setObjectType(ProductModelTypeEnum.DB.getName());

			persistent.setObjectName(tableName);

			persistent.setRemarks(table.getDescription());

			persistent.setPackageId(packageId);

			PRODUCT_MODEL_SERVICE.addNewProductModelObject(persistent);

			DatabaseTableColumnBean[] columns = table.getColumnBeans();

			for (int j = 0; j < columns.length; j++) {

				DatabaseTableColumnBean column = columns[j];

				ProductModelPropertyBean property = new ProductModelPropertyBean();

				property.setSubmitterId(submitterId);

				property.setObjectId(persistent.getId());

				property.setPropertyName(column.getName().toUpperCase());

				property.setRemarks(column.getDescription());

				property.setSortIndex(j);

				property.setDataType(column.getType());

				property.setDataLength(Integer.valueOf(column.getSizeAsInt()));

				property.setDataAccuracy(Integer.valueOf(column.getScale()));

				if (StringUtils.isNotBlank(column.getDefaultValue())) {
					property.setDefaultValue(column.getDefaultValue());
				}

				if (column.isAutoIncrement()) {
					property.setIsAutoInc("true");
				} else {
					property.setIsAutoInc("false");
				}

				if (column.isRequired()) {
					property.setIsAllowNull("false");
				} else {
					property.setIsAllowNull("true");
				}

				if (column.isPrimaryKey()) {
					property.setIsPrimaryKey("true");
				} else {
					property.setIsPrimaryKey("false");
				}

				PRODUCT_MODEL_SERVICE.addNewProductModelProperty(property);

			}

		} else {

			modelObjectBeans[0].setRemarks(table.getDescription());

			PRODUCT_MODEL_SERVICE.modifyProductModelObject(modelObjectBeans[0]);

			ProductModelPropertyBean[] propertyBeans = PRODUCT_MODEL_SERVICE
					.getProductPropertiesByObjectId(modelObjectBeans[0].getId());

			Map<String, ProductModelPropertyBean> propertyMap = new HashMap<String, ProductModelPropertyBean>();

			for (int j = 0; j < propertyBeans.length; j++) {
				ProductModelPropertyBean objectPropertyBean = propertyBeans[j];
				propertyMap.put(objectPropertyBean.getPropertyName().toUpperCase(), objectPropertyBean);
			}

			DatabaseTableColumnBean[] columns = table.getColumnBeans();

			ProductModelPropertyBean property = null;

			boolean isUpdate = true;

			for (int j = 0; j < columns.length; j++) {
				DatabaseTableColumnBean column = columns[j];

				if (propertyMap.containsKey(column.getName().toUpperCase())) {

					property = (ProductModelPropertyBean) propertyMap.get(column.getName().toUpperCase());

					isUpdate = true;

				} else {

					isUpdate = false;

					property = new ProductModelPropertyBean();

					property.setObjectId(modelObjectBeans[0].getId());

					property.setPropertyName(column.getName().toUpperCase());

				}

				property.setSubmitterId(submitterId);

				property.setSortIndex(j);

				property.setRemarks(column.getDescription());

				property.setDataType(column.getType());

				property.setDataLength(column.getSizeAsInt());

				property.setDataAccuracy(column.getScale());

				if (StringUtils.isNotBlank(column.getDefaultValue())) {
					property.setDefaultValue(column.getDefaultValue());
				}

				if (column.isAutoIncrement()) {
					property.setIsAutoInc("true");
				} else {
					property.setIsAutoInc("false");
				}

				if (column.isRequired()) {
					property.setIsAllowNull("false");
				} else {
					property.setIsAllowNull("true");
				}

				if (column.isPrimaryKey()) {
					property.setIsPrimaryKey("true");
				} else {
					property.setIsPrimaryKey("false");
				}

			}

			if (isUpdate == true) {
				PRODUCT_MODEL_SERVICE.modifyProductModelProperty(property);
			} else {
				PRODUCT_MODEL_SERVICE.addNewProductModelProperty(property);
			}

		}

	}

}
